﻿using System;
using My.Ioc;

namespace GetNotificationOfRegistrationAndUnregistration
{
    public class Dump { }

    class Program
    {
        static void Main(string[] args)
        {
            IObjectRegistration dumpRegistration;
            IObjectContainer container = new ObjectContainer(false);

            // Hook events
            container.ObjectBuilderRegistered += OnObjectBuilderRegistered;
            container.ObjectBuilderUnregistering += OnObjectBuilderUnregistering;

            // register
            container.Register<Dump>().Return(out dumpRegistration);
            // commit: ObjectBuilderRegistered should trigger upon this operation
            container.CommitRegistrations();
            // unregister: ObjectBuilderUnregistering should trigger
            container.Unregister(dumpRegistration);

            Console.ReadLine();
        }

        static void OnObjectBuilderRegistered(ObjectBuilderRegisteredEventArgs args)
        {
            if (args.ContractType == typeof(Dump))
                Console.WriteLine(args.ConcreteType.Name + " registered...");
        }

        static void OnObjectBuilderUnregistering(ObjectBuilderUnregisteringEventArgs args)
        {
            if (args.ContractType == typeof(Dump))
                Console.WriteLine(args.ConcreteType.Name + " unregistering...");
        }
    }
}
